package com.hmdp.listener;

import com.hmdp.config.RabbitMQConfig;
import com.hmdp.entity.VoucherOrder;
import com.hmdp.service.IVoucherOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class VoucherListener {

    @Autowired
    private IVoucherOrderService voucherOrderService;

    @RabbitListener(queues = RabbitMQConfig.VOUCHER_QUEUE)
    public void handleVoucherMessage(String message) {
        log.info("接收到RabbitMQ消息: {}", message);
        // 在这里处理接收到的消息
        // 例如：更新数据库、发送通知等
        try {
            // 将消息转换为VoucherOrder对象
            Long orderId = Long.valueOf(message);
            VoucherOrder voucherOrder = voucherOrderService.getById(orderId);
            if (voucherOrder != null) {
                voucherOrderService.createVoucherOrder(voucherOrder);
            }
        } catch (Exception e) {
            log.error("处理RabbitMQ消息失败: {}", e.getMessage());
        }
    }
}